<template>
  <div>
    <div class="header">
      <img src="@/assets/images/return.png" class="header_img" @click="back"/>
      <div class="title">我的教育经历</div>
      <div class="save" @click="saveBack">保存</div>
    </div>
    <div class="base_message">
      <div class="inputGroup">
        <div class="input_title">学校</div>
        <input type="text" placeholder="请输入学校名称" class="input_value" v-model="data.school"/>
      </div>
      <div class="inputGroup">
        <div class="input_title">专业</div>
        <input type="text" placeholder="请输入专业名称" class="input_value" v-model="data.specialty"/>
      </div>
      <div class="inputGroup">
        <div class="input_title">学历</div>
        <input type="text" placeholder="请输入学历" class="input_value" v-model="data.qualification"/>
      </div>
      <div class="inputGroup" @click="open('timePicker',1)">
        <div class="input_title">开始时间</div>
        <div class="input_value">{{data.startTime | formatDate('YYYY-MM-DD')}}</div>
      </div>
      <div class="inputGroup" @click="open('timePicker',2)">
        <div class="input_title">结束时间</div>
        <div class="input_value">{{ data.endTime | formatDate('YYYY-MM-DD')}}</div>
      </div>
    </div>
    <div class="base_message">
      <div class="work_describe">
        <div class="work_title">经历描述</div>
        <div class="work_text"><text-area classProp="experiment_text" placeholder="请从主要工作内容、成就等方面，描述你的工作经历，字数不少于20字" @sendVal="setValue"></text-area></div>
      </div>
    </div>
    <mt-datetime-picker
      ref="timePicker"
      type="date"
      :endDate="endDate"
      v-model="currentDate"
      :startDate="startDate"
      @confirm="handleChange">
    </mt-datetime-picker>
  </div>
</template>

<script>
  import TextArea from './TextArea'
  import { Toast} from 'mint-ui'
  var Moment = require('moment')
  export default {
    name: "Education",
    components: {
      TextArea
    },
    data() {
      return {
        endDate:new Date(),
        currentDate: new Date(),
        startDate: new Date(1970,1,1),
        pickerType:null,
        data:{
          startTime: null,
          endTime: null,
          school:null,
          specialty:null,
          qualification:null,
          experienceDesc:null
        }
      }
    },
    methods: {
      back() {
        this.$router.back()
      },
      setValue(text){
        this.data.experienceDesc = text
      },
      open(picker,type) {
        this.pickerType = type;
        this.$refs[picker].open();
      },
      saveBack(){
        if(!this.data.school){
          Toast({
            message: '请输入学校名称',
            position: 'middle',
            duration: 1000
          });
        }else if(!this.data.specialty){
          Toast({
            message: '请输入专业',
            position: 'middle',
            duration: 1000
          });
        }else if(!this.data.qualification){
          Toast({
            message: '请输入学历',
            position: 'middle',
            duration: 1000
          });
        }else if(!this.data.startTime){
          Toast({
            message: '请输入开始时间',
            position: 'middle',
            duration: 1000
          });
        }else if(!this.data.endTime){
          Toast({
            message: '请输入结束时间',
            position: 'middle',
            duration: 1000
          });
        }else if(!this.data.experienceDesc){
          Toast({
            message: '请输入您的经历描述',
            position: 'middle',
            duration: 1000
          });
        }else{
          Moment.locale('zh-cn');
          this.data.startTime = Moment(this.data.startTime.toString()).format('YYYY-MM-DD')
          this.data.endTime =  Moment(this.data.endTime.toString()).format('YYYY-MM-DD')
          this.$store.dispatch("action_addEduction", this.data)
          this.$router.back()
        }
      },
      handleChange(value) {
        switch (this.pickerType) {
          case 1:
            if( value.getTime() > new Date().getTime()){
              Toast({
                message: '开始时间不能大于当前时间',
                position: 'middle',
                duration: 1000
              });
            }else if(this.data.endTime && value.getTime() > this.data.endTime.getTime() ){
              Toast({
                message: '开始时间不能大于结束时间',
                position: 'middle',
                duration: 1000
              });
            }else{
              this.data.startTime = value
            }
            break
          case 2:
            if( value.getTime() > new Date().getTime()){
              Toast({
                message: '结束时间不能大于当前时间',
                position: 'middle',
                duration: 1000
              });
            }else if(this.data.startTime && value.getTime() < this.data.startTime.getTime() ){
              Toast({
                message: '结束时间不能小于开始时间',
                position: 'middle',
                duration: 1000
              });
            }else{
              this.data.endTime = value
            }
            break
        }
      },
    }
  }
</script>

<style scoped>
  .header {
    box-sizing: border-box;
    position: relative;
    display: flex;
    height: 90px;
    background-color: #8ec43e;
    align-items: center;
    padding: 0 30px;
  }

  .header_img {
    width: 17px;
    height: 30px;
  }

  .title {
    text-align: center;
    flex: 1;
    font-size: 30px;
    color: #ffff;
  }

  .save {
    font-size: 28px;
    color: #ffffff;
  }

  .base_message {
    margin-bottom: 25px;
  }

  .inputGroup {
    border-bottom: 1px solid #efefef;
    box-sizing: border-box;
    padding: 36px 30px;
    background-color: #ffffff;
    display: flex;
    align-items: center;
    height: 98px;
  }

  .input_title {
    font-size: 28px;
    font-weight: 400;
    color: rgba(43, 43, 43, 1);
  }

  .input_value {
    text-align: right;
    flex: 1;
    font-size: 28px;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
  }

  .input_value::placeholder {
    font-size: 28px;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
  }

  .checkbox {
    position: relative;
    box-sizing: border-box;
    display: flex;
    width: 30px;
    height: 30px;
    border: 2px solid rgba(153, 153, 153, 1);
    border-radius: 5px;
  }

  .flex {
    flex: 1;
  }

  .selected_img {
    position: absolute;
    left: -2px;
    top: -2px;
    transition: all 0.1s linear;
    opacity: 0;
    height: 30px;
    width: 30px;
  }

  .selected_show {
    opacity: 1;
  }

  .work_describe {
    background-color: #ffffff;
    padding: 31px 35px 31px 30px;
  }

  .work_title {
    font-size: 28px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(43, 43, 43, 1);
  }

  .work_text {
    padding: 30px 0 0 1px;
  }
  .experiment_text{
    padding: 8px;
    width: 100%;
    font-size: 24px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
    line-height: 36px;
    word-spacing: 5px;
  }
</style>
